package com.yylany.service;

import com.yylany.bo.GoodsBOBase;
import com.yylany.mapper.TbCategoryMapper;
import com.yylany.mapper.TbSkuMapper;
import com.yylany.pojo.TbCategory;
import com.yylany.utils.Verification;
import com.yylany.vo.GoodsInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

/**
 * Description: pinyougou
 *
 * @author <a href="mailto:17777502615@163.com">yaoyonglong</a>
 * @date 2019/1/2
 * @since 1.0.0
 */
@Service
public class GoodsInfoService {

    @Autowired
    private TbSkuMapper skuMapper;

    @Autowired
    private TbCategoryMapper categoryMapper;


    /**
     * 获取商品的信息
     * @param skuid
     * @return
     */
    public GoodsInfo getGoodInfo(Integer skuid) {
        //创建空对象
        GoodsInfo bese = new GoodsInfo();
        // 对skuid 进行判空，避免执行的时候错误
        if (skuid == null) {
            return bese;
        }
        //1 基本数据
        GoodsBOBase goodBaseInfo = skuMapper.getGoodBaseInfo(skuid);
        // 进行数据的拷贝操作
        BeanUtils.copyProperties(goodBaseInfo, bese);
        // 填充specInfo
        Verification.TbSkuTOGoodsInfo(bese, goodBaseInfo);
        // 适配分类的信息
        bese.setCat1Info(getTbVategory(goodBaseInfo.getCat1Id()));
        bese.setCat2Info(getTbVategory(goodBaseInfo.getCat2Id()));
        bese.setCat3Info(getTbVategory(goodBaseInfo.getCat3Id()));





        return bese;
    }

    /**
     * 根据id获取到分类信息
     * @param id
     * @return
     */
    public TbCategory getTbVategory(Integer id) {
        Example example = new Example(TbCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);
        TbCategory tbCategory = categoryMapper.selectOneByExample(example);
        return tbCategory;
    }

}
